/// Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.VSPowerToys.PackInstaller
{

    /// <summary>
    /// This class contains wrappers for Win32 API methods.
    /// </summary>
    class NativeMethods
    {
        [DllImport("user32")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool SendMessage(IntPtr hWnd, int msg,
          int wParam, int lParam);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool FlashWindowEx([MarshalAs(UnmanagedType.Struct)]
	        ref FLASHWINFO pfwi);

        private const int WM_SETREDRAW = 0xB;

        [StructLayout(LayoutKind.Sequential)]
        struct FLASHWINFO
        {
            [MarshalAs(UnmanagedType.U4)]
            public int cbSize;
            public IntPtr hwnd;
            [MarshalAs(UnmanagedType.U4)]
            public int dwFlags;
            [MarshalAs(UnmanagedType.U4)]
            public int uCount;
            [MarshalAs(UnmanagedType.U4)]
            public int dwTimeout;
        }

        const int FLASHW_STOP = 0;
        const int FLASHW_CAPTION = 0x00000001;
        const int FLASHW_TRAY = 0x00000002;
        const int FLASHW_ALL = (FLASHW_CAPTION | FLASHW_TRAY);
        const int FLASHW_TIMER = 0x00000004;
        const int FLASHW_TIMERNOFG = 0x0000000C;



        /// <summary>
        /// Hidden constructor
        /// </summary>
        private NativeMethods()
        {
        }

        /// <summary>
        /// Suspends painting of the specified control
        /// </summary>
        /// <param name="control"></param>
        public static void SuspendPainting(Control control)
        {
            if (control.IsHandleCreated && control.Visible)
            {
                SendMessage(control.Handle, WM_SETREDRAW, 0, 0);
            }
        }

        /// <summary>
        /// Resumes painting of the specified control
        /// </summary>
        /// <param name="control"></param>
        public static void ResumePainting(Control control)
        {
            if (control.IsHandleCreated && control.Visible)
            {
                SendMessage(control.Handle, WM_SETREDRAW, 1, 0);
                control.Invalidate(true);
            }
        }

        /// <summary>
        /// Makes the taskbar item for the specified window flash a variable number of times.
        /// </summary>
        /// <param name="windowToFlash"></param>
        /// <param name="numberOfTimesToFlash" />
        public static void FlashTaskbarWindow(Form windowToFlash, int numberOfTimesToFlash)
        {
            FLASHWINFO fi = new FLASHWINFO();
            fi.cbSize = Marshal.SizeOf(typeof(FLASHWINFO));
            fi.hwnd = windowToFlash.Handle;
            fi.dwFlags = FLASHW_TRAY;
            fi.uCount = numberOfTimesToFlash;
            fi.dwTimeout = 0;
            FlashWindowEx(ref fi);
        }
    }
}
